package com.szh.lenovo0518;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class MyOpenHelper extends SQLiteOpenHelper {
    public MyOpenHelper(@Nullable Context context) {
        super(context, "book.db", null, 2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table book(_id integer primary key autoincrement,name varchar(10) not null," +
                "author varchar(10))";
        db.execSQL(sql);
        ContentValues values = new ContentValues();
        values.put("name", "活着");
        values.put("author", "余华");
        db.insert("book", null, values);
        String sql2 = "insert into book(name,author)values(?,?)";
        db.execSQL(sql2, new Object[]{"霍乱时期的爱情", "加西亚·马尔克斯"});
        db.execSQL(sql2, new Object[]{"白夜行", "东野圭吾"});
        db.execSQL(sql2, new Object[]{"西游记", "吴承恩"});
        db.execSQL(sql2, new Object[]{"水浒传", "施耐庵"});
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "create table user(_id integer primary key autoincrement,username varchar(10) not null," +
                "age integer,phone varchar(10))";
        db.execSQL(sql);
        String sql2 = "insert into user(username,age,phone)values(?,?,?)";
        db.execSQL(sql2, new Object[]{"沈不愁", "18", "10010"});
        db.execSQL(sql2, new Object[]{"陈某乐", "2", "10086"});
        db.execSQL(sql2, new Object[]{"陈某行", "19", "10001"});
        db.execSQL(sql2, new Object[]{"邵氏", "22", "110"});
    }
}
